#!/bin/bash
########################################################################
# script name:  tl7_upgrade_procedure.func
# Used for:
# Author:       Martin Dumont
# Contributors:
# Version:      0.1
#
# (date format: date +'%Y/%m/%d')
#
# Date          Version Contributors    Modifications
# ----------    ------- ------------    -------------
# 2012/05/02    0.1     MDUMONT         initial
########################################################################
#
# This function file is used to executes
# the steps in the TL7 upgrade document
#
########################################################################
#
# The order specify the execution order in the main script
# 1 will be executed before 2
# The value tells the main script two things:
#    1) the function to execute (be specific)
#    2) comment (documentation, for better understanding in the logs)
#       --- Do not put spaces ---
########################################################################
########################################################################
# Order_a array gives the right order of functions execution to the stepping script
# The first field is the function name and the second is a comment
########################################################################
Order_a=( 'copy_files:Copying configuration files' 'start_automount:Start automount deamon' 'change_sec:Change Shells' 'install_ssh:Update SSH and SSL' 'refresh_syslog:Restart syslog' 'create_saunix:Create SAUNIX account' 'create_users:Create admins user accounts' 'root_login:Remove remote root login' 'update_baconf:Update /etc/BA.conf' 'add_AC_keys:Add SAPLXS01 public key' 'root_home:Change root home' 'todo:Step to execute after' 'add_crontabs:Adding crontab entries' )
#######################################
# Variables
#######################################
function set_variables {
	x=0
	while [ $x -lt 1 ]
	do
		read -p "What is the landscape (sb, dev, qa, int, prd, util)? "
		/bin/echo
		if [ -z "$REPLY" ]; then /bin/echo "Please choose a LANDSCAPE"; continue; fi
		LAND=$(/bin/echo $REPLY | tr '[A-Z]' '[a-z]')
		read -p "What is the SID? "
		/bin/echo
		if [ -z "$REPLY" ]; then /bin/echo "Please choose a SID"; continue; fi
		SID=$(/bin/echo $REPLY | tr '[A-Z]' '[a-z]')
		x=1
	done
	if [ -z "$LAND" ]; then /bin/echo "\$LAND not defined"; /bin/echo EXIT; fi
	if [ -z "$SID" ]; then /bin/echo "\$SID not defined"; /bin/echo EXIT; fi
	/bin/echo "Landscape is $LAND and SID is $SID"
}
#######################################
# functions
#######################################
#######################################
function copy_files {
	dir=/tmp/temp_$RANDOM
	mkdir $dir
	if ! mount core:/depot $dir; then /bin/echo EXIT; fi
	if ! cp $dir/files/etc_auto_master_prod /etc/auto_master; then /bin/echo EXIT; fi
	if ! cp $dir/files/etc_auto_central_prod /etc/auto_central; then /bin/echo EXIT; fi
	if ! cp $dir/files/etc_auto_home_prod /etc/auto_home; then /bin/echo EXIT; fi
	if ! cp $dir/files/etc_kshrc_prod /etc/kshrc; then /bin/echo EXIT; fi
	if ! cp $dir/files/etc_motd_prod /etc/motd; then /bin/echo EXIT; fi
	if ! cp $dir/files/etc_profile_prod /etc/profile; then /bin/echo EXIT; fi
	if ! cp $dir/files/etc_sudoers_prod /etc/sudoers; then /bin/echo EXIT; fi
	if ! cp $dir/files/etc_zlogout_prod /etc/zlogout; then /bin/echo EXIT; fi
	if ! cp $dir/files/root_dot_profile_prod /.profile; then /bin/echo EXIT; fi
	if ! cp $dir/files/syslog.conf.$LAND /etc/syslog.conf; then /bin/echo EXIT; fi
	if ! cp $dir/files/etc_ntp.conf /etc/ntp.conf; then /bin/echo EXIT; fi
	umount $dir && rmdir $dir
}
#######################################
#######################################
function start_automount {
	/usr/sbin/automount -v
	if [ $? != 0 ]; then /bin/echo EXIT; fi
}
#######################################
#######################################
function change_sec {
	chsec -f /etc/security/login.cfg -s usw -a shells=/bin/sh,/bin/bsh,/bin/csh,\
/bin/ksh,/bin/tsh,/bin/ksh93,/usr/bin/sh,/usr/bin/bsh,/usr/bin/csh,/usr/bin/ksh,\
/usr/bin/tsh,/usr/bin/ksh93,/usr/bin/rksh,/usr/bin/rksh93,/usr/sbin/uucp/uucico,\
/usr/sbin/sliplogin,/usr/sbin/snappd,/usr/bin/bash,/bin/bash,/usr/bin/zsh,\
/bin/zsh,/bin/git-shell,/bin/false
	if [ $? != 0 ]; then /bin/echo EXIT; fi
}
#######################################
#######################################
function install_ssh {
	if ! cd /central/depot/softwares/SSH; then /bin/echo Cannot enter directory /central/depot/softwares/SSH; /bin/echo EXIT; fi
	installp -a -d . -Y openssl.base openssh.base.client openssh.base.server; stopsrc -s sshd && sleep 2 && startsrc -s sshd
}
#######################################
#######################################
function refresh_syslog {
	mkdir -p /var/adm/syslog && touch /var/adm/syslog/syslog.log && /bin/refresh -s syslogd
	if [ $? != 0 ]; then /bin/echo EXIT; fi
}
#######################################
#######################################
function create_saunix {
	if ! grep -q "^saunix:" /etc/passwd
	then
		/usr/sbin/useradd -d /home/saunix -m -c 'Unix Super-Admin' -s /usr/bin/bash -u 4018 -g 1 saunix
	fi
	test -d ~saunix/.ssh || mkdir -m 700 ~saunix/.ssh ; cat /central/depot/files/saunix_pub_key > ~saunix/.ssh/authorized_keys; chmod 600 ~saunix/.ssh/authorized_keys; chown -R saunix ~saunix
}
#######################################
#######################################
function create_users {
	/usr/sbin/usermod -s /bin/false git
	/usr/sbin/useradd -d /home/b0450848 -m -c 'Kamel Soumeur/kamel.soumeur@aero.bombardier.com/SAP' -s /usr/bin/ksh -u 4003 -g 1 b0450848
	/usr/sbin/useradd -d /home/b0464936 -m -c 'Marek Silski/marek.silski@aero.bombardier.com/SAP' -s /usr/bin/ksh -u 4004 -g 1 b0464936
	/usr/sbin/useradd -d /home/b0463152 -m -c 'Guy Bourgeois/guy.bourgeois@aero.bombardier.com/SAP' -s /usr/bin/ksh -u 4005 -g 1 b0463152
	/usr/sbin/useradd -d /home/b0528436 -m -c 'Benoit Vezina/benoit.vezina@aero.bombardier.com/SAP' -s /usr/bin/ksh -u 4006 -g 1 b0528436
	/usr/sbin/useradd -d /home/b0075909 -m -c 'Joachim Romberg/joachim.romberg@aero.bombardier.com/SAP' -s /usr/bin/ksh -u 4007 -g 1 b0075909
	/usr/sbin/useradd -d /home/b0515131 -m -c 'German Kudinov/german.kudinov@aero.bombardier.com/Unix' -s /usr/bin/ksh -u 4008 -g 1 b0515131
	/usr/sbin/useradd -d /home/b0454716 -m -c 'Robert Leblanc/robert.leblanc@aero.bombardier.com/Unix' -s /usr/bin/ksh -u 4009 -g 1 b0454716
	/usr/sbin/useradd -d /home/b0456098 -m -c 'Jalal Azoui/jalal.azoui@aero.bombardier.com/SAP' -s /usr/bin/ksh -u 4010 -g 1 b0456098
	/usr/sbin/useradd -d /home/b0201459 -m -c 'John Carson/john.carson@aero.bombardier.com/SAP' -s /usr/bin/ksh -u 4011 -g 1 b0201459
	/usr/sbin/useradd -d /home/b0088553 -m -c 'Helmut Thievessen/helmut.thievessen@aero.bombardier.com/SAP' -s /usr/bin/ksh -u 4012 -g 1 b0088553
	/usr/sbin/useradd -d /home/b0525067 -m -c 'Dale Aston/dale.aston@aero.bombardier.com/SAP' -s /usr/bin/ksh -u 4013 -g 1 b0525067
	/usr/sbin/useradd -d /home/b0455977 -m -c 'Ali Boudda/ali.boudaa@aero.bombardier.com/SAP' -s /usr/bin/ksh -u 4014 -g 1 b0455977
	/usr/sbin/useradd -d /home/b0464989 -m -c 'Andree Gaudreau/andree.gaudreau@@aero.bombardier.com/SAP' -s /usr/bin/bash -u 4015 -g 1 b0464989
	/usr/sbin/useradd -d /home/b0464977 -m -c 'Bao Tran - SAP Core' -s /usr/bin/ksh -u 4016 -g 1 b0464977
	/usr/sbin/useradd -d /home/b0150245 -m -c 'Velvet Dao/Velvet.Dao@aero.bombardier.com/SAP Core' -s /usr/bin/bash -u 4017 -g 1 b0150245
	/usr/sbin/useradd -d /home/b0150378 -m -c 'Basim Borai/basim.borai@aero.bombardier.com/SAP' -s /usr/bin/bash -u 4021 -g 1 b0150378
	/usr/sbin/useradd -d /home/b0463631 -m -c 'Ed Tomlinson/edward.tomlinson@aero.bombardier.com/Solution Design' -s /usr/bin/ksh -u 4033 -g 1 b0463631
	/usr/sbin/useradd -d /tmp/b0528817 -c 'Martin_Dumont/martin.dumont@aero.bombardier.com/Unix' -s /bin/zsh -u 4040 -g 1 b0528817
	/usr/sbin/useradd -d /tmp/b0077592 -c 'Shervin Araghi/shervin.araghi@aero.bombardier.com/Printing' -s /bin/zsh -u 4002 -g 1 b0077592
	/usr/sbin/useradd -d /tmp/b0527740 -c 'Domenico Ferrara/domenico.ferrara@aero.bombardier.com/Unix' -s /bin/zsh -u 4001 -g 1 b0527740
	/usr/sbin/usermod -d /sapnfs/b0528817 b0528817
	/usr/sbin/usermod -d /sapnfs/b0077592 b0077592
	/usr/sbin/usermod -d /sapnfs/b0527740 b0527740
}
#######################################
#######################################
function root_login {
	/bin/echo "Removing root login: \c"
	/usr/local/scripts/remove_root_login localhost
	if [ $? != 0 ]; then /bin/echo EXIT; fi
}
#######################################
#######################################
function update_baconf {
	/bin/echo "Updating file /etc/BA.conf: \c"
	/usr/local/scripts/update_baconf.sh -h localhost
	if [ $? != 0 ]; then /bin/echo EXIT; fi
}
#######################################
#######################################
function add_AC_keys {
	/bin/echo "Updating file /home/saunix/.ssh/authorized_keys: \c"
	if ! /bin/echo 'ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAIEArxU0LOhIKVZkMCkBBrIQmUIYXcsKR6oXrwoU1JR6Yx98yn2cqOh3tmPFbROtRvt+3TACBWzAC3u52BBNIk+v5bvp3J6me+Z4EsBzTh7UWJOz62B7F7DAQjpwpIHKVkrw12ey3CXkq+nusxnyOfPB73bOt9xETteFHbWa1Cci3i8= saunix@saplxs01' >> /home/saunix/.ssh/authorized_keys; then /bin/echo "Something's wrong"; else /bin/echo ok; fi
}
#######################################
#######################################
function root_home {
	/bin/echo "Changing root home to /root: \c"
	/usr/local/scripts/change_root_home
	if [ $? != 0 ]; then /bin/echo EXIT; fi
}
#######################################
#######################################
function todo {
	/bin/echo "What needs to be done from saprmt:"
	/bin/echo "(cd /tmp; sudo /usr/local/scripts/sync_keys.sh -h saphii01 -u b0527740,b0077592,b0450848,b0464936,b0463152,b0528436,b0075909,b0515131,b0454716,b0456098,b0201459,b0088553,b0525067,b0455977,b0464989,b0464977,b0150245,b0150378,b0463631,b0528817)"
	/bin/echo "`uname -n`:aix:$LAND:$SID:n:n:y:n:y:y:build on `date`" > /central/logs/tmp/`uname -n`_hostconfig.tmp
	/bin/echo "/usr/local/scripts/hostconfig.sh -f /central/logs/tmp/`uname -n`_hostconfig.tmp"
}
#######################################
#######################################
function add_crontabs {
	/bin/echo "Please verify crontab"
}
#######################################
